﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.SingleWell.Services;
using Microsoft.Practices.Unity;
using ePlanet.SingleWell.Models;
using Web.SystemConfig.SysCache;
using Web.SystemConfig.WorkFlow;
using DreamBuilder.Core.Container;
using ePlanet.SingleWell.Business;
using Web.SystemConfig.Models;
using Web.SystemConfig.Services;
using Web.SystemConfig.Business;
using Web.SystemLogin.Models;
using System.Data;

namespace Web.Views.SingleWellManage
{
    public partial class SingleWellView1 : PageBase
    {
        #region 页面变量
        [Dependency]
        public ICodeTable CodeTableService
        {
            get;
            set;
        }
        [Dependency]
        public ISingleWellService m_ISingleWellService
        {
            get;
            set;
        }

        [Dependency]
        public IAttachmentService m_IAttachmentService
        {
            get;
            set;
        }

        private User m_CurrentUser
        {
            get
            {
                return (User)Session["CurrentUser"];
            }
        }

        private string m_DataID
        {
            get
            {
                return Request.QueryString["DataID"];
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    if (string.IsNullOrEmpty(this.m_DataID))
                    {
                        throw new Exception("参数DataID为空");
                    }
                    SingleWellModel oModel = this.m_ISingleWellService.GetSingleWellModel(this.m_DataID);
                    this.BindSingleData(oModel);
                    this.BindWellDetail(oModel);
                    this.BindAttachment(oModel);
                    WriteUserLog(string.Format("用户{0}查看了ID:{1}的单井数据", m_CurrentUser.Real_Name, this.m_DataID), OracleLogType.SingleWell);
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        #region 私有方法
        private bool BindAttachment(SingleWellModel oModel)
        {
            try
            {
                DataTable oDataTable = null;
                this.m_IAttachmentService.Initialize("SingleWell");
                SingleWellModel.SINGLEWELLRow singleRow = (SingleWellModel.SINGLEWELLRow)oModel.SINGLEWELL.Rows[0];
                if (singleRow == null)
                {
                    return true;
                }
                List<AttachmentModel> modelList = this.m_IAttachmentService.GetAttachmentModelByObjectID(singleRow.DATAID);
                if (modelList != null && modelList.Count > 0)
                {
                    oDataTable = new DataTable();
                    oDataTable.Columns.Add("attachmentid");
                    oDataTable.Columns.Add("attachmentname");
                    oDataTable.Columns.Add("uploadtime");
                    oDataTable.Columns.Add("attachmentsize");

                    foreach (AttachmentModel model in modelList)
                    {
                        DataRow dr = oDataTable.NewRow();
                        dr["attachmentid"] = model.AttachmentID;
                        dr["attachmentname"] = model.AttachmentName;
                        dr["uploadtime"] = model.UpdateTime;
                        dr["attachmentsize"] = model.AttachmentSize;

                        oDataTable.Rows.Add(dr);
                    }
                }
                if (oDataTable != null && oDataTable.Rows.Count > 0)
                {
                    this.gv_AttachMent.DataSource = oDataTable;
                    this.gv_AttachMent.DataBind();
                }
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private bool BindWellDetail(SingleWellModel oModel)
        {
            try
            {
                DataTable oDataTable = oModel.WELLDATADETAIL as DataTable;
                this.gv_SingleWellDetail.DataSource = oDataTable;
                this.gv_SingleWellDetail.DataBind();
                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private bool BindSingleData(SingleWellModel oModel)
        {
            try
            {
                SingleWellModel.SINGLEWELLRow singleRow = (SingleWellModel.SINGLEWELLRow)oModel.SINGLEWELL.Rows[0];
                if (!singleRow.IsA0Null())
                {
                    this.txtA0.Text = singleRow.A0.ToString();
                }
                if (!singleRow.IsA3Null())
                {
                    this.txtA3.Text = singleRow.A3.ToString();
                }
                if (!singleRow.IsA4Null())
                {
                    this.txtA4.Text = singleRow.A4.ToString();
                }
                if (!singleRow.IsBEGINDATENull())
                {
                    this.txtBEGINDATE.Text = singleRow.BEGINDATE.ToShortDateString();
                }
                if (!singleRow.IsBEGINWORKDATENull())
                {
                    this.txtBeginWorkDate.Text = singleRow.BEGINWORKDATE.ToShortDateString();
                }
                if (!singleRow.IsFINISHDATENull())
                {
                    this.txtFINISHDATE.Text = singleRow.FINISHDATE.ToShortDateString();
                }
                if (!singleRow.IsFINISHWORKDATENull())
                {
                    this.txtFinishWorkDate.Text = singleRow.FINISHWORKDATE.ToShortDateString();
                }
                if (!singleRow.IsWELLDEPTHNull())
                {
                    this.txtWellDepth.Text = singleRow.WELLDEPTH.ToString();
                }
                if (!singleRow.IsDATAFILENAMENull())
                {
                    this.txtDatafileName.Text = singleRow.DATAFILENAME;
                }
                if (!singleRow.IsWELLTYPENull())
                {
                    this.txtWellType.Text = singleRow.WELLTYPE;
                }
                if (!singleRow.IsWORKMODENull())
                {
                    this.txtWorkMode.Text = singleRow.WORKMODE;
                }

                this.txtWellCode.Text = singleRow.WELLCODE;
                /*****************************************
                 * 
                 * add by guoyin 2014-3-26
                 * 增加了井类别和类别明细
                 * 
                 *****************************************/
                if (!singleRow.IsCATEGORYNull())
                {

                    this.txtCategory.Text = CodeTableService.GetpkNameByCodepkValue(singleRow.CATEGORY); ;
                }
                if (!singleRow.IsCATEGORYDETAILNull())
                {
                    this.txtCateGoryDetail.Text = CodeTableService.GetpkNameByCodepkValue(singleRow.CATEGORYDETAIL); ;
                }
                /*END**/

                return true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}